﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="Page-Enter" content="revealTrans(duration=0.5, transition=8)" />
    <link href="../Comm/JS/Calendar/skin/WdatePicker.css" rel="stylesheet" type="text/css" />
    <link href="../Scripts/bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
    <link href="../Scripts/bootstrap/css/style.min.css" rel="stylesheet" type="text/css"
          media="all" />


    <link href="../Scripts/bootstrap/css/font-awesome.css" rel="stylesheet" />
    <link href="../Scripts/bootstrap/bootstrap-table/src/bootstrap-table.css" rel="stylesheet" type="text/css" />

    <!--jquery-->
    <script src="../Scripts/jquery-1.11.0.min.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/js/bootstrap.min.js" type="text/javascript"></script>

    <!--bootstrap-table-->
    <script src="../Scripts/bootstrap/bootstrap-table/src/bootstrap-table.js" type="text/javascript"></script>
    <script src="../Scripts/bootstrap/bootstrap-table/src/locale/bootstrap-table-zh-CN.js"
            type="text/javascript"></script>
    <script src="../Scripts/bootstrap/BootstrapUIDialog.js" type="text/javascript"></script>

    <!--公共JS-->
    <script src="../Scripts/QueryString.js" type="text/javascript"></script>
    <script src="../Scripts/config.js" type="text/javascript"></script>
    <script src="Gener.js" type="text/javascript"></script>
    <script language="JavaScript" src="JScript.js" type="text/javascript"></script>
    <script src="../Comm/JS/Calendar/WdatePicker.js" type="text/javascript"></script>
    <script src="../Scripts/vue.js" type="text/javascript"></script>


    <style type="text/css">
        .GroupTitle {
            padding: 8px;
            line-height: 24px;
            vertical-align: top;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            background-color: #eaf2ff;
            color: #0e2d5f;
            font-szie: 12px;
        }

        .form-control {
            width: auto;
        }

        .Wdate {
            border: 1px solid #e5e6e7;
            height: 34px;
        }

        .table-responsive {
            min-height: .01%;
            overflow-x: visible;
        }

        th {
            text-align: center;
        }
    </style>
    <script type="text/javascript">

        var newRowIdx = -1;
        var frmData;
        var mapData;
        var dtM; //方法集合.
        var webUser = new WebUser();
        var mapAtrrs;
        var InsertFlag = 0;
        var PK;//En的主键
        var newPKVal = 0;
        $(function () {
             var httpHandler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");
             httpHandler.AddUrlData();  //增加参数属性.

            var data = httpHandler.DoMethodReturnString("Ens_Init");
            if (data.indexOf('err@') == 0) {
                $("#Msg").html(data);
                return;
            }
            //解析json.
            frmData = JSON.parse(data);
            mapData = frmData["Sys_MapData"][0];
            mapData = new Entity("BP.Sys.MapData", mapData); //把他转化成entity.

            document.title = mapData.Name;

            //toolBar
            if (mapData.GetPara("IsInsert") == "1")
                $("#new_btn").show();
            else
                $("#new_btn").hide();

            if (mapData.GetPara("IsDelete") == "1")
                $("#delete_btn").show();
            else
                $("#delete_btn").hide();

             PK = mapData.GetPara("PK");
             BindTable();
        })

        //生成查询页面..
        function BindTable() {

            mapAtrrs = frmData.Sys_MapAttr;
            var dtls = frmData.Ens;
            //获取页面的高度
            var H = document.body.clientHeight;
            var tableH = H - $("#toolbar").outerHeight() - 30;

            var columns = new Array();;
            columns.push({
                title: '序',
                field: 'xu',
                align: 'center',
                width: 100,
                formatter: function (value, row, index) {
                    var rowIndx = index + 1;
                    return "<div style='width:30px'><input id='IDX_" + row[PK] + "' type='checkbox' name='IDX_" +  row[PK] + "' />"+rowIndx+"</div>";    // 返回每条的序号： 每页条数 *（当前页 - 1 ）+ 序号
                }
            });
            for (var i = 0; i < mapAtrrs.length; i++) {
                var attr = mapAtrrs[i];

                if (attr.UIVisible == 0
                    || attr.KeyOfEn == "OID"
                    || attr.KeyOfEn == "WorkID"
                    || attr.KeyOfEn == "NodeID"
                    || attr.KeyOfEn == "MyNum"
                    || attr.KeyOfEn == "MyPK") {
                    
                    keyOfEn = attr.KeyOfEn
                    continue;
                }


                var field = attr.KeyOfEn;

                var width = attr.Width;
                if (width < 60) {
                    width = 60;
                }
                if (field == "Title") {
                    width = 230;
                }
                var title = attr.Name;

                if (width == null || width == "" || width == undefined)
                    width = 100;


                columns.push({
                    field: field,
                    title: title,
                    width: width,
                    fixed: false,
                    formatter: function (value, row, index) {
                        return InitMapAttrOfCtrl(this.field, row, row[PK]);
                    }
                });
            }
             columns.push({
                    field: 'viewDetail',
                    title: '详细信息',
                    width: 80,
                    align: 'center',
                    formatter: function (value, row, index) {
                        return "<a href='#' onclick='OpenEn(\"" + row[PK] + "\")'>详情</a>";
                    }
                });



           
            $('#mytable').html("");
            $('#mytable').bootstrapTable({
                data: dtls,
                columns: [columns],
                cache: false,
                striped: true,
                height: tableH,
                singleSelect: true,
                sidePagination: "server",
                pageNumber: 1,
                strictSearch: true,
               

            });

            firstLoadTable = true;

        }

       

        function GetEntityByKey(key) {
            for (var i = 0; i < mapAtrrs.length; i++) {
                if (mapAtrrs[i].KeyOfEn == key)
                    return mapAtrrs[i];
            }
        }
        function InitMapAttrOfCtrl(field, dtl, pkval) {

            //根据field获取mapAttr
            var mapAttr = GetEntityByKey(field);
            var str = '';
            var defValue = ConvertDefVal(mapAttr, dtl);
            var isInOneRow = false; //是否占一整行
            var islabelIsInEle = false; //
            var eleHtml = '';

            //外部数据源类型.
            if (mapAttr.LGType == "0" && mapAttr.MyDataType == "1" && mapAttr.UIContralType == "1") {

                //枚举类型.
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                return "<select class='form-control' id='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            //外键类型.
            if (mapAttr.LGType == "2" && mapAttr.MyDataType == "1") {

                //枚举类型.
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                return "<select class='form-control' id='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "'  name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            //外部数据类型.
            if (mapAttr.LGType == "0" && mapAttr.MyDataType == "1" && mapAttr.UIContral == 1) {

                //枚举类型.
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                return "<select class='form-control' id='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            //添加文本框 ，日期控件等
            //AppString
            if (mapAttr.MyDataType == "1" && mapAttr.UIContralType == 0) {  //不是外键

                //  alert(mapAttr.UIWidth);


                if (mapAttr.UIHeight <= 40) //普通的文本框.
                {
                    var enableAttr = '';
                    if (mapAttr.UIIsEnable == 0)
                        enableAttr = "disabled='disabled'";

                    return "<input class='form-control' maxlength=" + mapAttr.MaxLen + "  name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' id='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' value='" + defValue + "'  type='text'  " + enableAttr + " />";
                }

                //普通的大块文本.
                return "<textarea class='form-control' maxlength=" + mapAttr.MaxLen + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' id='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='width:" + mapAttr.UIWidth + "px' type='text'  " + (mapAttr.UIIsEnable == 1 ? '' : ' disabled="disabled"') + " />"
            }

            //日期类型.
            if (mapAttr.MyDataType == 6) {
                var enableAttr = '';
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = 'onfocus="WdatePicker({dateFmt:' + "'yyyy-MM-dd'})" + '";';
                else
                    enableAttr = "disabled='disabled'";

                return "<input  class=' Wdate form-control' " + enableAttr + " value='" + defValue + "' id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'  name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' type='text' />";
            }

            //时期时间类型.
            if (mapAttr.MyDataType == 7) {

                var enableAttr = '';
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = 'onfocus="WdatePicker({dateFmt:' + "'yyyy-MM-dd HH:mm'})" + '";';
                else
                    enableAttr = "disabled='disabled'";

                return "<input class=' Wdate form-control' id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' type='text' value='" + defValue + "' style='width:120px;' " + enableAttr + "  name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' />";
            }

            // boolen 类型.
            if (mapAttr.MyDataType == 4) {  // AppBoolean = 7

                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                //CHECKBOX 默认值
                var checkedStr = '';
                if (checkedStr != "true" && checkedStr != '1') {
                    checkedStr = ' checked="checked" ';
                }

                checkedStr = "";

                return "<input  id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' " + enableAttr + " value='" + defValue + "' " + (defValue == 1 ? "checked='checked'" : "") + " type='checkbox' id='CB_" + pkval + "_" + mapAttr.KeyOfEn + "'  name='CB_" + pkval + "_" + mapAttr.KeyOfEn + "' " + checkedStr + "  /><label for='CB_" + pkval + "_" + mapAttr.KeyOfEn + "' ></label>";
            }

            //枚举类型.
            if (mapAttr.MyDataType == 2 && mapAttr.LGType == 1) { //AppInt Enum
                if (mapAttr.UIIsEnable == 1)
                    enableAttr = "";
                else
                    enableAttr = "disabled='disabled'";

                return "<select class='form-control' id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' name='DDL_" + pkval + "_" + mapAttr.KeyOfEn + "' " + (mapAttr.UIIsEnable == 1 ? '' : 'disabled="disabled"') + ">" + InitDDLOperation(frmData, mapAttr, defValue) + "</select>";
            }

            // AppDouble  AppFloat
            if (mapAttr.MyDataType == 5 || mapAttr.MyDataType == 3) {

                var enableAttr = '';
                if (mapAttr.UIIsEnable != 1)
                    enableAttr = "disabled='disabled'";

                return "<input class='form-control'  id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='text-align:right;' value='" + defValue + "'  onkeyup=" + '"' + "if(isNaN(value)) execCommand('undo')" + '"' + " onafterpaste=" + '"' + "if(isNaN(value))execCommand('undo')" + '"' + " maxlength=" + mapAttr.MaxLen / 2 + "   type='text'" + enableAttr + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'/>";
            }

            if ((mapAttr.MyDataType == 2)) { //AppInt
                var enableAttr = '';
                if (mapAttr.UIIsEnable == 0 || mapAttr.KeyOfEn == "NodeID") {
                    enableAttr = "disabled='disabled'";
                }
                return "<input class='form-control'  id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='text-align:right;' value='" + defValue + "' onkeyup=" + '"' + "if(isNaN(value) || (value%1 !== 0))execCommand('undo')" + '"' + " onafterpaste=" + '"' + "if(isNaN(value) || (value%1 !== 0))execCommand('undo')" + '"' + " maxlength=" + mapAttr.MaxLen / 2 + "   type='text'" + enableAttr + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'/>";
            }

            //AppMoney  AppRate
            if (mapAttr.MyDataType == 8) {
                var enableAttr = '';
                if (mapAttr.UIIsEnable == 1) {

                } else {
                    enableAttr = "disabled='disabled'";
                }
                return "<input class='form-control' id=name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "' style='text-align:right;' value='" + defValue + "' onkeyup=" + '"' + "if(isNaN(value))execCommand('undo')" + '"' + " onafterpaste=" + '"' + "if(isNaN(value))execCommand('undo')" + '"' + " maxlength=" + mapAttr.MaxLen / 2 + "   type='text'" + enableAttr + " name='TB_" + pkval + "_" + mapAttr.KeyOfEn + "'/>";
            }

            alert(mapAttr.Name + "的类型没有判断.");
            return;
        }

        //填充默认数据
        function ConvertDefVal(attr, dtl) {

            //通过MAINTABLE返回的参数
            for (var ele in dtl) {
                if (attr.KeyOfEn == ele && dtl != '') {
                    result = dtl[ele];
                    break;
                }
            }

            if (result != undefined && typeof (result) == 'string') {

            }
            return result = unescape(result);
        }

        //初始化下拉列表框的OPERATION
        function InitDDLOperation(flowData, mapAttr, defVal) {

            var operations = '';
            //外键类型.
            if (mapAttr.LGType == 2) {

                var data = frmData[mapAttr.KeyOfEn];
                if (data == undefined)
                    data = frmData[mapAttr.UIBindKey];

                if (data == undefined) {
                    alert('没有获得约定的数据源..' + mapAttr.KeyOfEn + " " + mapAttr.UIBindKey);
                    return;
                }



                $.each(data, function (i, obj) {
                    operations += "<option " + (obj.No == defVal ? " selected='selected' " : "") + " value='" + obj.No + "'>" + obj.Name + "</option>";
                });
            }


            //枚举类型.
            if (mapAttr.LGType == 1) {

                var enums = frmData.Sys_Enum;
                enums = $.grep(enums, function (value) {
                    return value.EnumKey == mapAttr.UIBindKey;
                });

                $.each(enums, function (i, obj) {
                    operations += "<option " + (obj.IntKey == defVal ? " selected='selected' " : "") + " value='" + obj.IntKey + "'>" + obj.Lab + "</option>";
                });
            }


            //外部数据源类型 MyFlowGener.js.InitDDLOperation
            if (mapAttr.LGType == 0) {
                var fn;
                try {
                    if (mapAttr.UIBindKey) {
                        fn = eval(mapAttr.UIBindKey);
                    }
                } catch (e) {
                }
                if (typeof fn == "function") {
                    $.each(fn.call(), function (i, obj) {
                        operations += "<option " + (obj.No == defVal ? " selected='selected' " : "") + " value='" + obj.No + "'>" + obj.Name + "</option>";
                    });
                } else if (typeof CommonHandler == "function") {
                    CommonHandler.call("", mapAttr.UIBindKey, function (data) {
                        GenerBindDDL("DDL_" + mapAttr.KeyOfEn, data, "No", "Name");
                    })
                } else {
                    alert('没有获得约定的数据源..' + mapAttr.KeyOfEn + " " + mapAttr.UIBindKey);
                }
            }
            return operations;
        }

        //新建.
        function New() {
            if ($("#new_btn").attr('disabled') == "disabled")
                return;
            var ensName = GetQueryString("EnsName");
            var enName = ensName.substring(0, ensName.length - 1);
            var httpHandler = new HttpHandler("BP.WF.HttpHandler.WF_CommEntity");
            httpHandler.AddPara("EnName", enName);
            var data = httpHandler.DoMethodReturnString("EntityOnly_Init");
            frmData = JSON.parse(data);
            mainData = frmData.MainTable[0];
            
            $('#mytable').bootstrapTable('insertRow', { index: $('#mytable').bootstrapTable('getData').length, row: mainData });
            $("#new_btn").attr('disabled', true);
            InsertFlag = 1;
            newPKVal = mainData[PK];

        }

        function OpenEn(pkVal) {

            var ensName = GetQueryString("EnsName");
            var enName = ensName.substring(0, ensName.length - 1);

            var url = "En.htm?EnName=" + enName + "&PKVal=" + pkVal + "&RefVal=" + GetQueryString("RefVal") + "&RefKey=" + GetQueryString("RefKey");

            OpenBootStrapModal(url, "eudlgframe", this.document.title + ' : 详细', 800, 450, "icon-property", null, null, null, function () {
                window.location.href = window.location.href;
            }, null, 'black');
        }

        function Delete() {
                  var ensName = GetQueryString("EnsName");
                if (window.confirm('您确定要删除吗？') == false)
                    return;

                //获得存储的值.
                var cbs = $("input[type='checkbox']:checked");
                var str = "";

                cbs.each(function () {
                    if (this.id.indexOf('IDX_') == -1)
                        return true;
                    str = this.id.substring(4);
                    var en = new Entity(ensName.substring(0, ensName.length - 1));
                    en.No = str;
                    en.MyPK = str;
                    en.WorkID = str;
                    en.NodeID = str;
                    en.Delete();

                });

                InitData(); //刷新.
        }
        function Save() {
           var handler = new HttpHandler("BP.WF.HttpHandler.WF_Comm");
            handler.AddUrlData();  //增加参数属性.
            formData = $("form").serialize();

            handler.AddFormData();
            handler.AddPara("InsertFlag", InsertFlag);
            handler.AddPara("NewPKVal", newPKVal);
            var data = handler.DoMethodReturnString("Entities_Save");
            
            alert(data);
            $("#newbtn").attr("disabled", false);
            InitData(); //刷新.
        }

        function SelectAll(obj) {
            var checkeds = $("input[type='checkbox']");
            if (obj.checked == true) {
                //获取name=box的复选框 遍历输出复选框
                for (var i = 0; i < checkeds.length; i++) {
                    checkeds[i].checked = true;
                }
            } else {
                for (var i = 0; i < checkeds.length; i++) {
                    checkeds[i].checked = false;
                }
            }

        }

        function InitData() {
            var handler = new HttpHandler("BP.WF.HttpHandler.WF_Comm")
            handler.AddPara("EnsName", GetQueryString("EnsName"));
            handler.AddPara("Paras", "@OrderBy="+PK);
            var data = handler.DoMethodReturnJSON("Ens_Init");
            $('#mytable').bootstrapTable("load", data);
            $("#new_btn").attr('disabled', false);
            InsertFlag = 0;
            newPKVal = 0;
        }


/**/</script>
</head>
<body>
    <form>
        <div class="wrapper wrapper-content animated fadeInRight" style="padding: 0px 10px 0px 10px">
            <div class="ibox-content" id="App">
                <div class="row">
                    <div class="panel panel-default" style="border: 0px; margin-bottom: 3px">
                        <div id="toolbar" style="width: 100%">
                            <a href="#" class="btn btn-default btn-sm" id="save_btn" onclick="Save()">保存</a>
                            <a href="#" class="btn btn-default btn-sm" id="new_btn"  onclick="New()">新建</a>
                            <a href="#" class="btn btn-default btn-sm" id="delete_btn"  onclick="Delete()">删除</a>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <table id="mytable" class="table table-hover" style="word-wrap: break-word; word-break: break-all;"></table>
                </div>
            </div>
            <div id="Msg">
            </div>
        </div>
    </form>

</body >

</html>